package com.taoyuan.hotel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.taoyuan.hotel.dao.RoomDao;
import com.taoyuan.hotel.entity.Room;
import com.taoyuan.hotel.service.intf.RoomService;
import com.taoyuan.hotel.vo.PageBean;
import com.taoyuan.hotel.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomDao dao;
    @Override
    public R save(Room room) {
        if (dao.add(room) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public PageBean queryPage(int page, int limit) {
        PageHelper.startPage(page,limit);
        PageInfo<Room> pageInfo = new PageInfo<Room>(dao.selectAll());
        return PageBean.bean(pageInfo.getTotal(),pageInfo.getList());
    }
    @Override
    public R del(int id) {
        if (id>0) {
            if (dao.del(id)>0) {
                return R.ok();
            }else {
                return R.fail();
            }
        }
        return R.fail();
    }
    @Override
    public R update(Room room) {
        if (dao.update(room)>0){
            return R.ok();
        } else{
            return R.fail();
        }
    }
    @Override
    public PageBean queryPageWhere(String roomtype, int page, int limit) {
        if (StringUtils.hasLength(roomtype)){
            roomtype = "%"+roomtype+"%";
        } else {
            roomtype=null;
        }
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<Room> pageInfo=new PageInfo<Room>(dao.selectWhere(roomtype));
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }
    @Override
    public R delBatch(int[] ids) {
        if(ids!=null && ids.length>0){
            //2.执行
            if(dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }
}
